package testing;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import matching.participant;


public class ParticipantCreator implements Runnable {

	String configFileS;
	String httpURL;
	int portListeningOn;
	int howManyParticipants;

	public ParticipantCreator(String _configFileS)
	{
		configFileS = _configFileS;
	}

	public void run() {

		Properties configFile = new Properties();
		try {
			configFile.load(new FileInputStream(configFileS));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		httpURL = "http://" + configFile.getProperty("URL") + ":" + configFile.getProperty("HTTPPORT");

		portListeningOn = Integer.parseInt(configFile.getProperty("PORTLISTENINGON"));
		howManyParticipants = Integer.parseInt(configFile.getProperty("HOWMANYPARTICIPANTS"));

		String[] splittArrayGives = configFile.getProperty("TOGIVE").split("\\,");
		String[] splittArrayTakes = configFile.getProperty("TOTAKE").split("\\,");

		if(splittArrayGives.length != splittArrayTakes.length)
		{
			System.out.println("No participants created. No the same number of gives and takes");
			return;
		}
		
		for(int i = 0; i < howManyParticipants; i++)
		{
			if(howManyParticipants == 0)
				return;
			else
			{
				if(i > 0)
				{
					portListeningOn += 1;
					participant p =new participant(httpURL, splittArrayGives[i], splittArrayTakes[i], portListeningOn);
					(new Thread(new createParticipant(p))).start();
				}
				else
				{
					participant p =new participant(httpURL, splittArrayGives[i], splittArrayTakes[i], portListeningOn);
					(new Thread(new createParticipant(p))).start();
				}
				
			}
				
		}



	}
	
	public class createParticipant implements Runnable
	{
		participant P;
		public createParticipant(participant _p)
		{
			P = _p;
		}
		
		public void run() {
			// TODO Auto-generated method stub
			P.start();
		}
		
	}

}
